import {
  createRouter,
  createWebHashHistory,
  createWebHistory,
} from "vue-router";
import { getisLogin, userInfo } from "@/api/index.js";
import { useUserStore } from "@/stores/user.js";

const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("@/views/login/index.vue"),
  },
  {
    path: "/entry",
    name: "entry",
    component: () => import("@/views/entry/index.vue"),
  },
  {
    path: "/index",
    name: "index",
    component: () => import("@/views/index/index.vue"),
  },
  {
    path: "/prompt",
    name: "prompt",
    component: () => import("@/views/prompt/index.vue"),
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes,
});
router.beforeEach(async (to, from, next) => {
  const dataStore = useUserStore();
  const url = window.location.href;
  if (url.includes("https://szjy.cqedu.cn:8081/")) {
    const token = localStorage.getItem("accessToken");
    if (token) {
      const data = await userInfo();
      console.log(data, "6.18");
      if (data.code === 200) {
        dataStore.userType = data?.data?.userInfo?.userType;
        dataStore.district = data?.data?.userInfo?.district;
        dataStore.switchAddress = data?.data?.userInfo?.district;
        if (to.path === "/login" && from.path === "/") {
          next({ path: "/index" });
        } else {
          next();
        }
      }
      next();
    } else {
      if (to.path === "/prompt") {
        next();
      } else if (to.path !== "/login") {
        next({ path: "/login" });
      } else {
        next();
      }
    }
  } else {
    console.log("nihao", to.path, from);
    // debugger;
    if (to.path.includes("/login&dcqc")) {
      // console.log("跳转");
      next();
      // debugger;
      // next({ path: "/index" });
      return;
    } else {
      // debugger;
      next();
    }
  }

  // debugger;
  // next();
});
export default router;
